// Copyright (c) 2025, 大连理工大学 (Dalian University of Technology)
//
// Licensed under the Mulan PSL v2.
// You can use this file according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of the License at
//
//     http://license.coscl.org.cn/MulanPSL2
//
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. SEE THE MULAN PSL v2
// FOR MORE DETAILS.

#include "Import/BDFImport/BulkData/Design/DLINKParser.h"

#include <algorithm>
#include <cstdint>
#include <iostream>

#include "Utility/Logging/SGLogger.h"

#include "DataStructure/Input/Design/DLINKData.h"

#include "DBManager/DBServiceFactory.h"
#include "DBManager/IService/Input/Design/IDLINKService.h"

#include "Import/BDFImport/BDFParserBase.h"

BULKDATA_ENTRY_PARSER_REG (DLINK);

using namespace SG::DBManager;
using namespace SG::Import;
using namespace SG::DataStructure;

enum DLINKIndex : std::uint8_t
{
    DLINKIndex_ID    = 1,
    DLINKIndex_DDVID = 2,
    DLINKIndex_C0    = 3,
    DLINKIndex_CMULT = 4
};

SG::DataStructure::Common::Status BDF::BULKDATA::ParseDLINK (const std::shared_ptr<SG::DBManager::DBServiceFactory>& dbServiceFactory,
                                                             std::vector<std::string>&                               dividedEntryInfo)
{
    SG::DataStructure::FEM::DLINKData tempData;  // 创建临时的DLINK数据对象

    for (size_t curRow = 1; curRow < 5; ++curRow)
    {
        if (is_all_space (dividedEntryInfo[curRow]))
        {
            continue;
        }

        switch (curRow)
        {
        case DLINKIndex_ID:
            tempData.m_id = convertToI64 (dividedEntryInfo[curRow]);
            break;
        case DLINKIndex_DDVID:
            tempData.m_DVID = convertToI64 (dividedEntryInfo[curRow]);
            break;
        case DLINKIndex_C0:
            tempData.m_C0 = convertToReal (dividedEntryInfo[curRow]);
            break;
        case DLINKIndex_CMULT:
            tempData.m_CMULT = convertToReal (dividedEntryInfo[curRow]);
            break;
        default:
            // 若都不是跳过
            break;
        }
    }

    // 计算词条行数
    size_t const rowNum = dividedEntryInfo.size () / 10;

    // 设置续行符下标
    size_t const endColumnIndex = 9;

    // 节点的第一行从index=5开始
    size_t beginColumnIndex = 5;

    for (size_t curRow = 0; curRow < rowNum; curRow++)
    {
        // 首先判断是不是第一行
        if (curRow != 0)
        {
            // 除了第一行以外，其他行的第一个字段为节点数据
            beginColumnIndex = 1;
        }

        // 遍历当前行的所有字段
        for (size_t fieldsColumn = beginColumnIndex; fieldsColumn < endColumnIndex; fieldsColumn += 2)
        {
            // 计算当前字段的索引
            const size_t curIndex = (curRow * 10) + fieldsColumn;

            // 获取当前字段IDVi和下一字段Ci的字符串
            std::string& curStrKey_IDVi = dividedEntryInfo[curIndex];
            std::string& curStrKey_Ci   = dividedEntryInfo[curIndex + 1];

            // 检查当前字段是否为空白
            bool const isEmpty_IDVi = is_all_space (curStrKey_IDVi);
            bool const isEmpty_Ci   = is_all_space (curStrKey_Ci);

            // 如果当前字段为空白，跳过处理
            if (isEmpty_IDVi || isEmpty_Ci)
            {
                continue;
            }

            // 插入到IDVCList中
            tempData.m_IDVCList.emplace_back (convertToI64 (curStrKey_IDVi), convertToReal (curStrKey_Ci));
        }
    }

    return dbServiceFactory->get<IDLINKService> ()->Add (tempData);
}